<?php

namespace App\Http\Controllers\Admin;

use App\Enums\ProductType;
use App\Http\Controllers\Controller;
use App\Models\Airdrop;

class AirdropBlindBoxController extends Controller
{
    /**
     * 盲盒空投记录.
     */
    public function index()
    {
        $query = Airdrop::query()
            ->where('type', ProductType::BLIND_BOX)
            ->when(request()->keyword, function ($q) {
                return $q->where(function ($q1) {
                    $likeVal = '%' . request()->keyword . '%';

                    return $q1->where('nickname', 'like', $likeVal)->orWhere('mobile', 'like', $likeVal)->orWhere('goods_name', 'like', $likeVal);
                });
            })
            ->when(request()->status, function ($q) {
                if (3 == request()->status) {
                    request()->status = 0;
                }

                return $q->where('status', request()->status);
            })
            ->when(request()->agent_id, function ($q) {
                return $q->where('agent_id', request()->agent_id);
            })
            ->orderByDesc('id');

        return paginate_result($query);
    }
}
